<?php
class quarkresolved{
	protected $id,$quark,$job,$jobsolution,$createdAt,$updatedAt;
	
	function __construct($id,$quarkid,$jobid,$jobsolutionid,$createdAt,$updatedAt){
		$this->id = $id;
		$this->quark = user::getUserById($quarkid);
		$this->job = job::getJobById($jobid);
		$this->jobsolution = jobsolution::getJobSolutionById($jobsolutionid);
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
	}
	
	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}

	public function getQuark(){
		return $this->quark;	
	}

	public function getJob(){
		return $this->job;	
	}

	public function getJobSolution(){
		return $this->jobsolution;	
	}

	public function getCreatedAt(){
		return $this->createdAt;	
	}

	public function getUpdatedAt(){
		return $this->updatedAt;	
	}

	public function setQuark($var){
		$this->quark = $var;
	}

	public function setJob($var){
		$this->job = $var;
	}

	public function setJobSolution($var){
		$this->jobsolution = $var;
	}

	public static function getQuarkResolvedByJobId($jobid){
		$conn = new dbmodifier();
		$jobid = $conn->db_connection->real_escape_string($jobid);
		$query = "SELECT id, quark_id, job_id, job_solution_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM quarkresolved WHERE job_id =".$jobid."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}
	
	public static function getQuarkResolvedByQuarkIdAndJobId($quarkid,$jobid){
		$conn = new dbmodifier();
		$quarkid = $conn->db_connection->real_escape_string($quarkid);
		$jobid = $conn->db_connection->real_escape_string($jobid);		
		$query = "SELECT id, quark_id, job_id, job_solution_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM quarkresolved WHERE quark_id =".
					$quarkid." AND job_id=".$jobid;
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();	
			$var = new quarkresolved($id,$row['quark_id'],$row['job_id'],$row['job_solution_id'],
					$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}
	
	public static function getQuarkResolvedById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT id, quark_id, job_id, job_solution_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM quarkresolved WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();	
			$var = new quarkresolved($id,$row['quark_id'],$row['job_id'],$row['job_solution_id'],
					$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}
	
	
	public static function createQuarkResolve($quarkid,$jobid,$jobsolutionid){
		$conn = new dbmodifier();
		$quarkid = $conn->db_connection->real_escape_string($quarkid);
		$jobid = $conn->db_connection->real_escape_string($jobid);
		$jobsolutionid = $conn->db_connection->real_escape_string($jobsolutionid);		
		$query = "INSERT INTO quarkresolved (quark_id,job_id,job_solution_id,created_at,updated_at) " .
				 " VALUES ('".$userid."','".$quarkid."',FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new quarkresolved($id,$quarkid,$jobid,$jobsolutionid,time(),time());
		return $var;		
	}
	
	public function save(){
		$conn = new dbmodifier();
		$quarkid = $conn->db_connection->real_escape_string($this->quark->getId());
		$jobid = $conn->db_connection->real_escape_string($this->job->getId());
		$jobsolutionid = $conn->db_connection->real_escape_string($this->jobsolution->getId());		
		$query = "UPDATE quarkresolved SET quark_id='".$quarkid."'";
		$query = $query.", job_id='".$jobid."'";
		$query = $query.", job_solution_id='".$jobsolutionid."'";		
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updated_at = time();		
	}
	
	public function delete(){
		$conn = new dbmodifier();
		$query = "DELETE FROM quarkresolved WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}

	public function deleteByQuarkId($qid){
		$conn = new dbmodifier();
		$query = "DELETE FROM quarkresolved WHERE quark_id=".$qid;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}
}
?>